﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.HVAC.AuxiliaryCurve;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	public class DrawBoostLine : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			List<ViewType> list = new List<ViewType>();
			list.Add(1);
			list.Add(2);
			list.Add(4);
			if (!YJKRevitTools.ViewCheck(commandData.Application.ActiveUIDocument, list))
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			Result result;
			try
			{
				if (commandData == null)
				{
					throw new System.ArgumentNullException();
				}
				Document document = commandData.Application.ActiveUIDocument.Document;
				FormDrawAuxiliaryCurve formDrawAuxiliaryCurve = new FormDrawAuxiliaryCurve(commandData);
				DialogResult dialogResult = DialogResult.OK;
				do
				{
					if (dialogResult == DialogResult.OK)
					{
						dialogResult = formDrawAuxiliaryCurve.ShowDialog();
					}
					if (dialogResult == DialogResult.Yes)
					{
						transaction = new Transaction(document, "绘制辅助线");
						transaction.Start();
						SelcetionFactory selcetionFactory = new SelcetionFactory(commandData, (AuxiliarySelectType)formDrawAuxiliaryCurve.nBoostPointType);
						try
						{
							selcetionFactory.Select();
						}
						catch (Exception)
						{
							transaction.Commit();
							transaction = null;
							formDrawAuxiliaryCurve.Dispose();
							return Autodesk.Revit.UI.Result.Succeeded;
						}
						if (!new AuxiliaryFactory(commandData, formDrawAuxiliaryCurve.m_Type, formDrawAuxiliaryCurve.xSide, formDrawAuxiliaryCurve.ySide, formDrawAuxiliaryCurve.xNumber, formDrawAuxiliaryCurve.yNumber, formDrawAuxiliaryCurve.xDis, formDrawAuxiliaryCurve.yDis, selcetionFactory.Min, selcetionFactory.Max, selcetionFactory.AnglePt, selcetionFactory.SelectPoint, selcetionFactory.SelectElement).CreateAuxiliaryCurve())
						{
							dialogResult = DialogResult.OK;
						}
						else
						{
							dialogResult = DialogResult.Yes;
						}
						transaction.Commit();
					}
				}
				while (dialogResult != DialogResult.Cancel);
				formDrawAuxiliaryCurve.Dispose();
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				message = ex.Message;
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (transaction != null)
				{
					TransactionStatus status = transaction.GetStatus();
					if (status == 1)
					{
						transaction.Commit();
					}
					else if (status == 5)
					{
						transaction.RollBack();
					}
				}
			}
			return result;
		}
	}
}
